package com.huawei.basic.controller;

import com.huawei.basic.dto.RegisterDto;
import com.huawei.basic.exception.BusinessException;
import com.huawei.basic.service.IVerifyCodeService;
import com.huawei.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 验证码接口
 * 1.图形验证码
 * 2.短信验证码
 * 3.邮箱验证码
 * 
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 发送图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult imageCode(@PathVariable("key")String key){
        
        try {
           String base64Str =  verifyCodeService.imageCode(key);
            return AjaxResult.success().setResultObj(base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMessage("操作失败！"+e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    
    /**
     * 发送图形验证码
     * @param 
     * @return
     */
    @PostMapping("/phone/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){

        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.success();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMessage("操作失败！"+e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}